<?php
/**
 * 后台共用方法  
 * AreaController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * -------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class AdminCommon extends CApplicationComponent {    
    /**
     * 模板风格列表
     * @param integer $disable 是否显示停用的{1:是,0:否}
     * @return array $
     */
    public function getTemplateList($disable = 0) {
        $aData = array();
    	$aList = glob(ROOT_DIR.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
    	foreach ($aList as $key=>$v) {
    		$dirname = basename($v);
    		if (file_exists($v.DIRECTORY_SEPARATOR.'config.php')) {
    			$aData[$key] = include $v.DIRECTORY_SEPARATOR.'config.php';
    			if (!$disable && isset($aData[$key]['disable']) && $aData[$key]['disable'] == 1) {
    				unset($aData[$key]);
    				continue;
    			}
    		} else {
    			$aData[$key]['name'] = $dirname;
    		}
    		$aData[$key]['dirname']=$dirname;
    	}
    	return $aData;
    }
    /**
     * AdminCommon::getCurTemplateList()
     * 
     * @param mixed $style
     * @param mixed $module
     * @return array 
     */
    public function getCurTemplateList($style, $module, $id = '', $str = '', $pre = ''){
        $tpl_root = 'themes';
		$templatedir = ROOT_DIR.DIRECTORY_SEPARATOR.$tpl_root.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
		$confing_path = ROOT_DIR.DIRECTORY_SEPARATOR.$tpl_root.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'config.php';
		$localdir = str_replace(array('/', '\\'), '', $tpl_root).'|'.$style.'|views|'.$module;
		$templates = glob($templatedir.'*.php');        
		if(empty($templates)) {
			$style = 'default';
			$templatedir = ROOT_DIR.DIRECTORY_SEPARATOR.$tpl_root.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
			$confing_path = ROOT_DIR.DIRECTORY_SEPARATOR.$tpl_root.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'config.php';
			$localdir = str_replace(array('/', '\\'), '', $tpl_root).'|'.$style.'|views|'.$module;
			$templates = glob($templatedir.'*.php');
		}
		if(empty($templates)) return false;
		$files = @array_map('basename', $templates);
		$names = array();
		if(file_exists($confing_path)) {
			$names = include $confing_path;
		}
        //return $files;
		$templates = array();
		if(is_array($files)) {
			foreach($files as $file) {
				$key = substr($file, 0, -4);
				$templates[$key] = isset($names['file_explan'][$localdir][$file]) && !empty($names['file_explan'][$localdir][$file]) ? $names['file_explan'][$localdir][$file].'('.$file.')' : $file;
			}
		}
		ksort($templates);
		return $templates;
    }
    /**
     * AdminCommon::getHandleTemplateList() 
     * 
     * @param string $sTemplate
     * @return
     */
    public function getHandleTemplateList($sTemplate){
        $aData = array('category'=>array() ,'list'=>array() ,'show'=>array());
        if( empty($sTemplate) ){
            return $aData;
        }
        $aTemplate = Yii::app()->admincommon->getCurTemplateList($sTemplate ,'content');
        if( empty($sTemplate) ){
            return $aData;
        }
        //获取配置文件
        $sConfigPath = ROOT_DIR.DIRECTORY_SEPARATOR.'themes'.DIRECTORY_SEPARATOR.$sTemplate.DIRECTORY_SEPARATOR.'config.php';
        if(file_exists($sConfigPath)) {
			$aTemplateConfig = require $sConfigPath;
            //获取内容配置
            $sLocal = 'theme|'.$sTemplate.'|views|content';
            if( isset($aTemplateConfig['file_explan'][$sLocal]) && $aTemplateConfig['file_explan'][$sLocal] ) $aTemplateConfig = $aTemplateConfig['file_explan'][$sLocal];
		}
        foreach( $aTemplate as $sKey=>$sVal ){
            if( strpos($sKey ,'category')===0 ){
                $aData['category'][$sKey] = isset($aTemplateConfig[$sKey.'.php']) ? "(".$aTemplateConfig[$sKey.'.php'].")" .$sVal : $sVal;
            } elseif( strpos($sKey ,'list')===0 ){
                $aData['list'][$sKey] = isset($aTemplateConfig[$sKey.'.php']) ? "(".$aTemplateConfig[$sKey.'.php'].")" .$sVal : $sVal;
            }elseif( strpos($sKey ,'show')===0 ){
                $aData['show'][$sKey] = isset($aTemplateConfig[$sKey.'.php']) ? "(".$aTemplateConfig[$sKey.'.php'].")" .$sVal : $sVal;
            } elseif( strpos($sKey ,'page')===0 ){
                $aData['page'][$sKey] = isset($aTemplateConfig[$sKey.'.php']) ? "(".$aTemplateConfig[$sKey.'.php'].")" .$sVal : $sVal;
            }
        }
        return $aData;
    } 
    /**
     * AdminCommon::getMysqlVersion() 获取数据库版本
     * 
     * @return float 返回版本信息
     */
    public function getMysqlVersion(){
        $oResult = Yii::app()->db->createCommand("select VERSION() as version")->query();
        if( !empty($oResult) ){
            foreach($oResult as $aVal){
              return $aVal['version'];
            } 
        }
        return 0;
        
    }
    
}
?>